<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 9:34
 */

namespace app\admin\controller;


use app\common\controller\AdminBase;
use app\common\model\Systems as systems_model;
use think\Db;

class Systemparameter extends AdminBase{

    protected $user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->systems_model = new systems_model();
    }


    /**
     * 参数配置
     * @return mixed
     */
    public function index()
    {
        $data = array();
        //获取系统参数的配置
        $data = $this->systems_model->getSystemData(1);
        if (!$data) {
            $data[0]['s_rmb_dollar_exchange'] = 0;
            $data[0]['s_weixin'] = 0;
            $data[0]['s_alipay'] = 0;
            $data[0]['s_second_level'] = 0;
            $data[0]['s_third_level'] = 0;
            $data[0]['s_fouth_level'] = 0;
            $data[0]['s_service_harge'] = 0;
            $data[0]['s_project_num'] = 0;
            return $this->fetch('system/systemParameter', ['data' => $data, 'status' => 1]);
        } else {
            return $this->fetch('system/systemParameter', ['data' => $data, 'status' => 2]);
        }
    }

    //添加  修改 系统配置参数
    public function updatedOrAdd()
    {
        $params = input();
        if ($params['status'] == 1) {
            //添加
            unset($params['status']);
            $params['s_weixin'] = $params['photo'][0];
            $params['s_alipay'] = $params['photo'][1];
            unset($params['photo']);
            unset($params['editorValue']);
            $res = $this->systems_model->addSystem($params);
            if (!$res) {
                return $this->error('添加失败');
            }
            return $this->success('添加成功');
        }
        if ($params['status'] == 2) {
            //修改
            unset($params['status']);
            $params['s_weixin'] = $params['photo'][0];
            $params['s_alipay'] = $params['photo'][1];
            unset($params['photo']);
            unset($params['editorValue']);
            $res = $this->systems_model->updateSystem($params, 1);
            if (!$res) {
                return $this->error('更新失败');
            }
            return $this->success('更新成功');
        }
    }

    /**
     * 会员配置信息列表
     */
    public function memberSystemList($page = 1)
    {
        $list = $this->systems_model->memberList($page);
        return $this->fetch('system/memberList', ['list' => $list, 'status' => 2]);
    }

    /**添加会员信息页面
     * @return mixed
     */
    public function memberpage()
    {
        return $this->fetch('system/memberadd');
    }

    /**
     * 添加会员信息
     */
    public function memberadd()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            unset($data['editorValue']);
            if (array_key_exists('photo', $data)) {
                $data['ms_photo'] = $data['photo'][0];
            }
            unset($data['photo']);
            $status = $this->systems_model->add($data);
            if ($status) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**会员详情
     * @param $id
     * @return mixed
     */
    public function detail($id)
    {
        $detail = $this->systems_model->memberdetail($id);
        return $this->fetch('system/detail', ['detail' => $detail]);
    }

    /**
     * 修改会员信息
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            unset($data['editorValue']);
            if (array_key_exists('photo', $data)) {
                $data['ms_photo'] = $data['photo'][0];
            }
            unset($data['photo']);
            $status = $this->systems_model->edit($data);
            if ($status) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    /**
     * 禁用启用
     * @param $id
     */
    public function editstatus($id, $status)
    {
        if ($status == 1) {
            $status = 2;
        } else if ($status == 2) {
            $status = 1;
        } else {
            $this->error('修改失败');
        }
        $status = $this->systems_model->editStatus($id, $status);
        if ($status) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    /**
     * 删除会员信息
     */
    public function delete($id)
    {
        $res = Db::name('member_system')->where('ms_id', $id)->delete();
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}